//
//  LGDeviceManageTableController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2025/2/24.
//

#import "LGDeviceManageTableController.h"
#import "LGDeviceOrderDetailController.h"
#import "LGDeviceManageTableCell.h"
#import "LGDeviceDeliverView.h"
#import "LGDeviceRecycleView.h"
@interface LGDeviceManageTableController ()<UITableViewDelegate,UITableViewDataSource,LGDeviceManageTableDelegate>

/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSMutableArray *dataArray;
/**   */
@property (nonatomic , copy)NSString *keyword;
/**   */
@property (nonatomic , copy)NSString *pageNo;

@end

@implementation LGDeviceManageTableController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = RandomColor;
    self.keyword = @"";
    self.gradientLayer.hidden = YES;
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.tableView frame:self.tableView.bounds];
    [self requestData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestData) name:self.notifi_name object:nil];
}

-(void)requestData{
    self.pageNo = @"1";
    self.dataArray = [NSMutableArray array];
    [self requestDataWithPage:@"1"];
}

-(void)requestMoreData{
    [self requestDataWithPage:self.pageNo];
}

-(void)requestDataWithKeyword:(NSString *)keyword{
    self.keyword = keyword;
    self.pageNo = @"1";
    [self requestDataWithPage:self.pageNo];
}

-(void)requestDataWithPage:(NSString *)pageNo{
    __block NSString *currentPage = pageNo;
    self.keyword = (_keyword && ![_keyword isEmptyString])?_keyword:@"";
    [RequestUtil withPOST:self.action parameters:@{@"pageNo":self.pageNo,@"pageSize":@"20",@"keyword":self.keyword,self.key_name:self.key_value} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if([currentPage isEqualToString:@"1"]){
            [self.dataArray removeAllObjects];
        }
        if(code == 200){
            //总数
            NSInteger total = [LGNSString(returnData[@"total"]) integerValue];
            if (self.delegate && [self.delegate respondsToSelector:@selector(totalNumberChanged:index:)]) {
                [self.delegate totalNumberChanged:total index:self.index];
            }
            
            NSArray *tempArray = returnData[@"data"];
            if(tempArray.count>0){
                self.pageNo = [NSString stringWithFormat:@"%ld",[returnData[@"current_page"] integerValue]+1];
                [self.dataArray addObjectsFromArray:tempArray];
            }else{
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        
        [self.tableView reloadData];

        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无数据" offset:0];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无数据" offset:0];
        }
    }];
}

//设备发货
-(void)deliverDeviceAction:(NSInteger)index{
    NSDictionary *tempDic = self.dataArray[index];
    NSString *recordid = [self.type isEqualToString:@"0"]?LGNSString(tempDic[@"id"]):LGNSString(tempDic[@"bus_id"]);
    NSString *action = [self.type isEqualToString:@"0"]?@"/doctor/equipment.equipment/receiptDetail":@"/doctor/equipment.equipment/mallDetail";
    NSDictionary *paramaDic = [self.type isEqualToString:@"0"]?@{@"receipt_id":recordid}:@{@"record_id":recordid};
    [RequestUtil withPOST:action parameters:paramaDic withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            LGDeviceDeliverView *deliverView = [[LGDeviceDeliverView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
            deliverView.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0.8];
            deliverView.type = self.type;
            deliverView.dataDic = returnData;
            [LGKeyWindow addSubview:deliverView];
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        
    }];
    
}

//设备回收
-(void)recycleDeviceAction:(NSInteger)index{
    NSDictionary *tempDic = self.dataArray[index];
    NSString *recordid = [self.type isEqualToString:@"0"]?LGNSString(tempDic[@"id"]):LGNSString(tempDic[@"bus_id"]);
    NSString *action = [self.type isEqualToString:@"0"]?@"/doctor/equipment.equipment/receiptDetail":@"/doctor/equipment.equipment/mallDetail";
    NSDictionary *paramaDic = [self.type isEqualToString:@"0"]?@{@"receipt_id":recordid}:@{@"record_id":recordid};
    [RequestUtil withPOST:action parameters:paramaDic withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            LGDeviceRecycleView *recycleView = [[LGDeviceRecycleView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
            recycleView.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0.8];
            recycleView.type = self.type;
            recycleView.dataDic = returnData;
            [LGKeyWindow addSubview:recycleView];
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        
    }];
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGDeviceManageTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if ([self.type isEqualToString:@"0"]) {
        cell.serviceDic = self.dataArray[indexPath.row];
    }else{
        cell.shopDic = self.dataArray[indexPath.row];
    }
    cell.index = indexPath.row;
    cell.delegate = self;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row < self.dataArray.count) {
        NSDictionary *tempDic = self.dataArray[indexPath.row];
        LGDeviceOrderDetailController *controller = [[LGDeviceOrderDetailController alloc]init];
        controller.type = self.type;
        controller.recordid = [self.type isEqualToString:@"0"]?LGNSString(tempDic[@"id"]):LGNSString(tempDic[@"bus_id"]);
        [self.navigationController pushViewController:controller animated:YES];
    }else{
        [TooltipView showMessage:@"请下拉刷新列表" offset:0];
    }
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGDeviceManageTableCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 1)];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestData)];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestMoreData)];
    }
    return _tableView;
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.tableView.frame = self.view.bounds;
}


@end
